package pers.wechat.pay.util;

import pers.myutil.local.encryption.MD5;

import java.util.Map;
import java.util.SortedMap;

public class WeChatPayUtil {

    /**
     * 创建Sign
     * @param packageParams 参数
     * @param key 秘钥
     * @return sign
     */
    public static String createSign(SortedMap<String,Object> packageParams,String key){

        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry entry : packageParams.entrySet()) {
            String k = String.valueOf(entry.getKey());
            String v = String.valueOf(entry.getValue());
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                stringBuilder
                        .append(k)
                        .append("=")
                        .append(v)
                        .append("&");
            }
        }
        stringBuilder.append("key=")
                .append(key);
        return MD5.getMd5(stringBuilder.toString());
    }
}
